package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.dao.OmPrincipalDao;
import com.bestcem.xm.om.dao.mapper.OmPrincipalMapper;
import com.bestcem.xm.om.entity.OmPrincipal;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 描述信息
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/28 15:39
 */
@Repository
public class OmPrincipalDaoImpl implements OmPrincipalDao {
    @Resource
    private OmPrincipalMapper omPrincipalMapper;

    @Override
    public List<OmPrincipal> findByOrgId(String orgId) {
        return omPrincipalMapper.findByOrgId(orgId);
    }

    @Override
    public OmPrincipal save(OmPrincipal omPrincipal) {
        LocalDateTime now = LocalDateTime.now();
        omPrincipal.setCreateTime(now);
        omPrincipal.setUpdateTime(now);
        omPrincipal.setId(ObjectId.next());
        omPrincipalMapper.save(omPrincipal);
        return omPrincipal;
    }

    @Override
    public void batchInsert(List<OmPrincipal> principals) {
        LocalDateTime now = LocalDateTime.now();
        for (OmPrincipal principal : principals) {
            principal.setCreateTime(now);
            principal.setUpdateTime(now);
            principal.setId(ObjectId.next());
        }
        omPrincipalMapper.batchInsert(principals);
    }

    @Override
    public Long removeByOrgId(String orgId) {
        return omPrincipalMapper.removeByOrgId(orgId);
    }

    @Override
    public List<OmPrincipal> findByName(String name) {
        return omPrincipalMapper.findByName(name);
    }

    @Override
    public List<OmPrincipal> findByOmUserId(String omUserId) {
        return omPrincipalMapper.findByOmUserId(omUserId);
    }

    @Override
    public List<OmPrincipal> listByCondition(OmPrincipal principal) {
        return omPrincipalMapper.listByCondition(principal);
    }

    @Override
    public boolean existsByOmUserIdAndOrgId(String omUserId, String orgId) {
        return omPrincipalMapper.existsByOmUserIdAndOrgId(omUserId, orgId) != 0;
    }

    @Override
    public List<OmPrincipal> findPrincipalByOrgIds(Set<String> orgIds) {
        return omPrincipalMapper.findPrincipalByOrgIds(orgIds);
    }

    @Override
    public List<OmPrincipal> listByOrgIds(List<String> orgIds) {
        return omPrincipalMapper.findPrincipalByOrgIds(new HashSet<>(orgIds));
    }

    @Override
    public Long countPrincipalByOrgIds(Set<String> orgIds) {
        return omPrincipalMapper.countPrincipalByOrgIds(orgIds);

    }

    @Override
    public List<OmPrincipal> pageByOrgIds(Set<String> orgIds, Integer currentPage, Integer size) {
        Integer offset = (currentPage - 1)* size;
        return omPrincipalMapper.pageByOrgIds(orgIds, offset, size);
    }

    @Override
    public List<String> listOrgIdsByName(String name) {
        return omPrincipalMapper.listOrgIdsByName(name);
    }
}
